package com.hainiu.cat.web.codeStudy.hutool;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.ConverterRegistry;
import cn.hutool.core.date.CalendarUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.CharsetUtil;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * create by biji.zhao on 2021/2/26
 */
public class TestConvert {

    public static void main(String[] args) {
//        testBaseConvert();
//        testCode();
//        testTimeAndPrice();
//        registryConvert();
//        testTime();

//        yyyy-MM-dd HH:mm:ss
//        yyyy-MM-dd
//        HH:mm:ss
//        yyyy-MM-dd HH:mm
//        yyyy-MM-dd HH:mm:ss.SSS

        String dateStr = "2017-03-01";
        Date date = DateUtil.parse(dateStr);
        System.out.println(date);

    }

    private static void testTime() {
        DateTime date = DateUtil.date();
        System.out.println(date);

        System.out.println(DateUtil.date(CalendarUtil.calendar()));

        System.out.println(DateUtil.date(System.currentTimeMillis()));

        System.out.println(DateUtil.now());

        System.out.println(DateUtil.today());
    }

    private static void registryConvert() {
        ConverterRegistry converterRegistry = ConverterRegistry.getInstance();
        //此处做为示例自定义String转换，因为Hutool中已经提供String转换，请尽量不要替换
        //替换可能引发关联转换异常（例如覆盖String转换会影响全局）
        converterRegistry.putCustom(String.class, CustomConverter.class);

        int a = 454553;
        String result = converterRegistry.convert(String.class, a);

        System.out.println(result);
    }

    private static void testTimeAndPrice() {
        long a = 4535345;

        System.out.println(Convert.convertTime(a, TimeUnit.MILLISECONDS, TimeUnit.MINUTES));

        System.out.println("================");

        double d = 210913.23;
        System.out.println(Convert.digitToChinese(d));
    }

    private static void testCode() {
        String a = "123456789";

        String s = Convert.toSBC(a);

        System.out.println(s);

        System.out.println(Convert.toDBC(s));

        String hexStr = "我是一个小小的可爱的字符串";

        String s1 = Convert.toHex(hexStr, CharsetUtil.CHARSET_UTF_8);

        System.out.println(s1);

        System.out.println(Convert.hexToStr(s1, CharsetUtil.CHARSET_UTF_8));

        System.out.println("============");

        String s2 = Convert.strToUnicode(hexStr);

        System.out.println(s2);

        System.out.println(Convert.unicodeToStr(s2));

        System.out.println("============");

        String code = "我不是乱码";

        String s3 = Convert.convertCharset(code, CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1);

        System.out.println(s3);

        System.out.println(Convert.convertCharset(s3, CharsetUtil.ISO_8859_1, CharsetUtil.UTF_8));
    }

    private static void testBaseConvert() {
        int a = 1;
        System.out.println(Convert.toStr(a));

        long[] b = {1,2,3,4};

        System.out.println(Convert.toStr(b));

        String[] s = {"1", "3", "7"};
        Integer[] integers = Convert.toIntArray(s);
        for (int i = 0; i < integers.length; i++) {
            System.out.println(integers[i]);
        }

        String date = "2020-02-26";

        Date date1 = Convert.toDate(date);
        System.out.println(date1);

        System.out.println(new Date());

        Object[] list = {"a", "你", "好", 1};

        List<String> ts = Convert.convert(new TypeReference<List<String>>() {}, list);

        ts.forEach(System.out::println);
    }
}
